package com.cinemtl.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cinemtl.action.UserAction;
import com.cinemtl.manager.UserManager;

/**
 * Servlet implementation class Login
 */
@WebServlet("/login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if (UserAction.verifyUser(request, response, username, password)) {
			HttpSession session = request.getSession();
				session.setAttribute("user",
						UserManager.getByUsername(username));
				response.getWriter().write(username);
			
		}
		 else {
			response.getWriter().write("invalid");
		}
	}
}
